app.register.controller('CotyReportCtrl', function($scope, $http, $rootScope,
		$route, CotyReportSer, AppUtil, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	
	//获取本地存储中的页码页数
	var page = AppUtil.GetLocalValue("cotyReportTable_Page");
	var size = AppUtil.GetLocalValue("cotyReportTable_Size");
	$scope.pagination = {
			totalItems :0,
			currentPage : page == undefined ? 1 : page,
			itemsPerPage : size == undefined ? 10 : size
	};
	
	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		LoadList();
	};
	// 加载品牌,车系,车型,颜色,配置,部门,销售顾问,订单类型,订单状态,是否按揭
	AppUtil.LoadGroups();
	$http.get("common/brands").success(function(data) {
		$scope.brands = data;
		if($scope.selected!=undefined){
			if($scope.selected.brand_code!=undefined)
				$scope.LoadSeries($scope.selected.brand_code);
			if($scope.selected.series_code!=undefined)
				$scope.LoadModels($scope.selected.series_code);
		}
	});
	$scope.LoadEntityBrands=function(entity){
		console.log('LoadEntityBrands');
		$http.get("common/LoadEntityBrands?entity_code="+entity.entity_code).success(function(data) {
			$rootScope.ch_brand="";
			$rootScope.brands = data;
		});
	};
	$scope.LoadBrandsCoty = function(group_name){
		$scope.entity_group_name=group_name;
		$("#entity_brand_name").val("");
		$("#entity_name").val("");
		$("#seriesname").val("");
		$("#modelname").val("");
		$scope.series = null;
		if(group_name==''){
			$http.get("common/groups").success(function(data) {
				//$scope.groups = data.gruops;
				$scope.gBrands = data.brands;
				$scope.entitys = data.entitys;
			});
			return;
		}
		$http.get("common/gBrands?group_name="+group_name).success(function(data) {
			$scope.gBrands = data;
			$scope.entitys = $scope.entitys;
		});
	};
	
	// 加载数据方法
		
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		params["brand_text"] = angular.element('select[name=brandcode]').find("option:selected").text();
		params["series_text"] = angular.element('select[name=seriescode]').find("option:selected").text();
		params["model_text"] = angular.element('select[name=modelname]').find("option:selected").text();
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		CotyReportSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.cotyReports = response.data;
			setTimeout(check_table_tr,500);
		});
	}
	// 配置分页基本参数
	$scope.$watch('pagination.refresh',LoadList);

	/*$http.get("cotyReport/dicts").success(function(data){
		$scope.brands=data.data.brands;
		$scope.series=data.data.series;
		$scope.models=data.data.models;
		$scope.colors=data.data.colors;
		$scope.configs=data.data.configs;
	});*/

	AppUtil.LoadGroups();
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
});

app.register.service('CotyReportSer', function($http,AppUtil) {
	this.list = function(postData) {
		AppUtil.loading();
		//设置查询开始下标
		postData.start= (postData.page-1<0?0:postData.page-1)*postData.size;
		return $http.post('cotyReport/list', postData).error(function() {
			AppUtil.remove_loading();
		});;
	};

});
